package com.jqb.webservice.client;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.ws.soap.MTOMFeature;

import org.junit.Before;
import org.junit.Test;

import webservice.jqb.com.upload.IUploadService;
import webservice.jqb.com.upload.UploadService;

public class UploadClient {
	private String webUrl = "http://localhost/test/upload?wsdl";
	private String ns = "http://com.jqb.webservice/upload";
	private String serviceName = "UploadService";
	private IUploadService port;

	@Before
	public void init() {
		try {
			URL url = new URL(webUrl);
			QName qname = new QName(ns, serviceName);
			UploadService uploadService = new UploadService(url, qname);
//			添加参数表示通过附件形式上传
			port = uploadService.getUploadServicePort(new MTOMFeature());
			/*如下方式也可以设置以附件形式上传
			port = uploadService.getUploadServicePort();
			BindingProvider bp = (BindingProvider)port;
			SOAPBinding soapBinding = (SOAPBinding)bp.getBinding();
			soapBinding.setMTOMEnabled(true);*/
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testUpload() {
		try {
			File f = new File("test/main/resources/t.jpg");
			/*方式一*/
//			byte[] file = FileUtils.readFileToByteArray(f);
			/*方式二*/
			DataHandler file = new DataHandler(new FileDataSource(f));
			port.upload(file);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
